package ace.module.oauth2.server.test.core.controller;

import ace.cmp.core.model.R;
import ace.cmp.security.core.impl.constant.Oauth2Constant;
import ace.module.oauth2.callback.contract.feign.Oauth2AceAuthorizationCallbackFeign;
import ace.module.oauth2.server.api.callback.model.input.Oauth2AceAuthorizationCallbackInput;
import ace.module.oauth2.server.api.model.dto.ext.Oauth2AceUserInfoDto;
import ace.module.oauth2.server.api.model.dto.ext.Oauth2AceUserInfoResultDto;
import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author caspar
 * @date 2023/2/14 10:58
 */
@RequestMapping("/upms/oauth2/rpc")
@RestController
@AllArgsConstructor
public class Oauth2AceAuthorizationCallbackTestController implements Oauth2AceAuthorizationCallbackFeign {

  @GetMapping("/kuser/test")
  public R<String> test(
      @RequestBody(required = false) String body,
      @RequestParam(value = "grantType", required = false) String grantType) {
    return R.ok(String.format("success,%s,%s", grantType, body));
  }

  @GetMapping("/kuser/test2")
  public R<String> test(String grantType) {
    return R.ok("/kuser/test2");
  }

  @Override
  public Oauth2AceUserInfoResultDto getUserInfo(Oauth2AceAuthorizationCallbackInput request) {
    Oauth2AceUserInfoResultDto resultDto = new Oauth2AceUserInfoResultDto();
    resultDto.setCode(R.OK);
    resultDto.setMessage("ok");
    resultDto.setData(
        Oauth2AceUserInfoDto.builder()
            .id(request.getAccountId().toString())
            .attributes(Map.of("getUserInfo", request.getAccountId().toString()))
            .authorities(
                Set.of(
                    "read",
                    "write",
                    Oauth2Constant.SCOPE_INTERNAL_CLIENT,
                    Oauth2Constant.SCOPE_USERINFO))
            .build());
    return resultDto;
  }
}
